//!
//! # HTTP Operations
//!

use myutil::{err::*, *};

/// 高创函数中检查 http 状态码
pub trait StatusChecker {
    /// Response 类型
    type Target;

    /// 200 原样返回, 否则返回错误信息
    fn check_status(self) -> Result<Self::Target>;
}

impl StatusChecker for attohttpc::Response {
    type Target = Self;

    fn check_status(self) -> Result<Self::Target> {
        let code = self.status().as_u16();

        if 200 == code {
            Ok(self)
        } else {
            Err(eg!(format!(
                "[Code: {}] [Detail: {}]",
                code,
                self.text().unwrap_or_default()
            )))
        }
    }
}
